package com.xxgc.vfc.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.xxgc.vfc.api.dto.UserLoginDTO;
import com.xxgc.vfc.api.result.Result;
import com.xxgc.vfc.api.vo.LoginByPhoneAndPassVO;
import com.xxgc.vfc.api.vo.UserRegisterByPhoneVO;
import com.xxgc.vfc.auth.service.IUsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;

/**
 * @Author: SJY
 * @Date: 2023/11/10 11:53:04
 * 注册 登录写了
 */
@Slf4j
@Api(tags = "认证中心")
@RestController
@RequestMapping("/auth")
public class LoginController {

    @Autowired
    private IUsersService iUsersService;

    @ApiOperation("手机号密码注册")
    @PostMapping("/registerByPhone")
    public Result registerByPhone(@RequestBody @Valid UserRegisterByPhoneVO userRegisterByPhoneVO) throws UnsupportedEncodingException {
        iUsersService.userRegisterByPhone(userRegisterByPhoneVO);
        return Result.ok("注册成功");
    }


    //手机号密码登录
    @ApiOperation("手机号密码登录")
    @PostMapping("/loginByPhoneAndPass")
    public Result loginByPhoneAndPass(
            @RequestBody @Valid LoginByPhoneAndPassVO loginByPhoneAndPassVO,
            HttpServletResponse httpServletResponse) throws UnsupportedEncodingException {

        UserLoginDTO userLoginDTO = iUsersService.loginByPhoneAndPass(loginByPhoneAndPassVO);
        // 第1步，先登录上
        StpUtil.login(userLoginDTO.getId());
        // 第2步，获取 Token  相关参数
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        log.info("token是：{}",tokenInfo);
        // 第3步 把token在响应头上给前端
        httpServletResponse.setHeader("authorization",tokenInfo.getTokenValue());
        return Result.ok(userLoginDTO).msg("登录成功");
    }


    //邮箱验证码登录（10分钟）



    //扫码登录




}
